// packageOrder/pages/order/order-detail.js
import OrderService from '../../../service/OrderService';
import { throttle, moment } from '../../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    isCompete: false,
    isFavorite: false,
    user: app.globalData.user
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _ = this;
    this.orderService = new OrderService();
    this.loadData(options.id);
    setTimeout(function () {
      _.orderService.browse(options.id);
    }, 3000);
  },
  /**打电话 */
  callPhone: function () {
    const _ = this;
    throttle(function () {
      const { activeScore } = app.globalData.user;
      const { user } = _.data;
      if (activeScore && activeScore > 1) {
        wx.showModal({
          title: '温馨提示',
          content: '拨打电话将扣去1枚救兵勋章',
          cancelText: '下次拨打',
          confirmText: '知道了',
          confirmColor: '#00accd',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.makePhoneCall({
                phoneNumber: user.phone //仅为示例，并非真实的电话号码
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: '救兵勋章不足，递名片、发红包、分享、写评论可获得',
          showCancel: false,
          confirmText: '知道了',
          confirmColor: '#00accd'
        })
      }
    }, 1000)();

  },
  /**工厂竞标 */
  doCompete: function() {
    const { order, isCompete} = this.data;
    if (isCompete){
      return;
    }
    const _ = this;
    throttle(function () {
      _.orderService.compete(order.id).then(function(res) {
        if (res.status == 0) {
          _.loadData(order.id);
          wx.showToast({
            title: '竞标申请成功',
            duration: 2000
          })
        }
      });
    }, 1000)()
  },
  /**收藏订单 */
  doFavorite: function () {
    const { order, isFavorite } = this.data;
    if (isFavorite) {
      return;
    }
    const _ = this;
    throttle(function () {
      _.orderService.favorite(order.id).then(function (res) {
        if (res.status == 0) {
          wx.showToast({
            title: '收藏成功',
            duration: 2000
          })
          _.loadData(order.id);
        }
      });
    }, 1000)()
  },
  loadData: function(id){
    const _ = this;
    _.orderService.getOrderById(id).then(function(res) {
      console.log(res);
      if (res.status == 0) {
        const { data } = res;
        data.createTime = moment(data.createTime).format("YYYY-MM-DD HH:mm")
        data.endTime = moment(data.endTime).format("YYYY-MM-DD")
        _.setData({ order: data });
        _.isCompete(data.id);
        _.isFavorite(data.id);
      }
    })
  },
  isCompete: function(id){
    const _ = this;
    this.orderService.isCompete(id).then(function(res){
      if(res.status == 0){
        _.setData({isCompete: res.data});
      }
    })
  },
  isFavorite: function(id){
    const _ = this;
    this.orderService.isFavorite(id).then(function (res) {
      if (res.status == 0) {
        _.setData({ isFavorite: res.data });
      }
    })
  },
  toMap: function(e){
    const _ = this;
    throttle(function () {
      const { activeScore } = app.globalData.user;
      const { user, order } = _.data;
      if (activeScore && activeScore > 0) {
        wx.showModal({
          title: '温馨提示',
          content: '拨打电话将扣去1枚救兵勋章',
          cancelText: '下次拨打',
          confirmText: '知道了',
          confirmColor: '#00accd',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              // wx.navigateTo({
              //   url: `../map/map?lat=${order.lat}&lng=${order.lng}`
              // })
              wx.openLocation({//​使用微信内置地图查看位置。
                latitude: order.lat,//要去的纬度-地址
                longitude: order.lng,//要去的经度-地址
                name: "位置",
                address: order.linkAddr
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: '救兵勋章不足，递名片、发红包、分享、写评论可获得',
          showCancel: false,
          confirmText: '知道了',
          confirmColor: '#00accd'
        })
      }
    }, 1000)();

  }

})